home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / post1.0 / ui / example-1dot1 / ContainerDemo.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.9 KB  |  126 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.applet.Applet;
  18. import java.awt.*;
  19. import java.awt.event.ContainerEvent;
  20. import java.awt.event.ContainerListener;
  21. import java.awt.event.ActionEvent;
  22. import java.awt.event.ActionListener;
  23. import java.util.Vector;
  24.  
  25. public class ContainerDemo extends Applet 
  26.                implements ContainerListener,
  27.                       ActionListener {
  28.     TextArea display;
  29.     Panel buttonPanel;
  30.     Button addButton, removeButton, clearButton;
  31.     Vector buttonList;
  32.  
  33.     public void init() {
  34.     //Initialize an empty list of buttons.
  35.     buttonList = new Vector(10, 10);
  36.  
  37.     //Create all the components.
  38.     addButton = new Button("Add a button");
  39.     addButton.addActionListener(this);
  40.  
  41.     removeButton = new Button("Remove a button");
  42.     removeButton.addActionListener(this);
  43.  
  44.     buttonPanel = new Panel();
  45.     buttonPanel.addContainerListener(this);
  46.  
  47.     display = new TextArea(5, 20);
  48.     display.setEditable(false);
  49.  
  50.     clearButton = new Button("Clear text area");
  51.     clearButton.addActionListener(this);
  52.  
  53.     //Lay out the components.
  54.     GridBagLayout gridbag = new GridBagLayout();
  55.     GridBagConstraints c = new GridBagConstraints();
  56.     setLayout(gridbag);
  57.     c.fill = GridBagConstraints.BOTH; //Fill entire cell.
  58.  
  59.         c.weighty = 1.0;  //Button area and message area have equal height.
  60.     c.gridwidth = GridBagConstraints.REMAINDER; //end of row
  61.     gridbag.setConstraints(display, c);
  62.     add(display);
  63.  
  64.         c.weighty = 0.0;  
  65.     gridbag.setConstraints(clearButton, c);
  66.     add(clearButton);
  67.  
  68.         c.weightx = 1.0;  //Add/remove buttons have equal width.
  69.     c.gridwidth = 1;  //NOT end of row
  70.     gridbag.setConstraints(addButton, c);
  71.     add(addButton);
  72.  
  73.     c.gridwidth = GridBagConstraints.REMAINDER; //end of row
  74.     gridbag.setConstraints(removeButton, c);
  75.     add(removeButton);
  76.  
  77.         c.weighty = 1.0;  //Button area and message area have equal height.
  78.     gridbag.setConstraints(buttonPanel, c);
  79.     add(buttonPanel);
  80.     }
  81.  
  82.     public void componentAdded(ContainerEvent e) {
  83.     displayMessage(" added to ", e);
  84.     }
  85.  
  86.     public void componentRemoved(ContainerEvent e) {
  87.     displayMessage(" removed from ", e);
  88.     }
  89.  
  90.     void displayMessage(String action, ContainerEvent e) {
  91.     display.append(((Button)e.getChild()).getLabel()
  92.                + " was"
  93.                + action
  94.                + e.getContainer().getClass().getName()
  95.                + "\n");
  96.     }
  97.  
  98.     /*
  99.      * This could have been implemented as two or three
  100.      * classes or objects, for clarity.
  101.      */
  102.     public void actionPerformed(ActionEvent e) {
  103.     Object source = e.getSource();
  104.  
  105.     if (source == addButton) {
  106.         Button newButton = new Button("Button #"
  107.                                       + (buttonList.size() + 1));
  108.         buttonList.addElement(newButton);
  109.         buttonPanel.add(newButton);
  110.         buttonPanel.validate(); //Make the button show up.
  111.     } else if (source == removeButton) {
  112.         int lastIndex = buttonList.size() - 1;
  113.         try {
  114.             Button nixedButton = (Button)buttonList.elementAt(lastIndex);
  115.             buttonPanel.remove(nixedButton);
  116.         buttonList.removeElementAt(lastIndex);
  117.             buttonPanel.validate(); //Make the button disappear.
  118.         } catch (ArrayIndexOutOfBoundsException exc) {
  119.         }
  120.     } else if (source == clearButton) {
  121.         display.setText("");
  122.     }
  123.     }
  124. }
  125.  
  126.